/* Copyright (c) 2024 Li Jin, dragon-fly@qq.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

extern "C" {
	fn rendertarget_type() -> i32;
	fn rendertarget_get_width(slf: i64) -> i32;
	fn rendertarget_get_height(slf: i64) -> i32;
	fn rendertarget_set_camera(slf: i64, var: i64);
	fn rendertarget_get_camera(slf: i64) -> i64;
	fn rendertarget_get_texture(slf: i64) -> i64;
	fn rendertarget_render(slf: i64, target: i64);
	fn rendertarget_render_clear(slf: i64, color: i32, depth: f32, stencil: i32);
	fn rendertarget_render_clear_with_target(slf: i64, target: i64, color: i32, depth: f32, stencil: i32);
	fn rendertarget_save_async(slf: i64, filename: i64, func: i32, stack: i64);
	fn rendertarget_new(width: i32, height: i32) -> i64;
}
use crate::dora::IObject;
/// A RenderTarget is a buffer that allows you to render a Node into a texture.
pub struct RenderTarget { raw: i64 }
crate::dora_object!(RenderTarget);
impl RenderTarget {
	pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
		(unsafe { rendertarget_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
			match raw {
				0 => None,
				_ => Some(Box::new(RenderTarget { raw: raw }))
			}
		})
	}
	/// Gets the width of the rendering target.
	pub fn get_width(&self) -> i32 {
		return unsafe { rendertarget_get_width(self.raw()) };
	}
	/// Gets the height of the rendering target.
	pub fn get_height(&self) -> i32 {
		return unsafe { rendertarget_get_height(self.raw()) };
	}
	/// Sets the camera used for rendering the scene.
	pub fn set_camera(&mut self, var: &dyn crate::dora::ICamera) {
		unsafe { rendertarget_set_camera(self.raw(), var.raw()) };
	}
	/// Gets the camera used for rendering the scene.
	pub fn get_camera(&self) -> Option<crate::dora::Camera> {
		return unsafe { crate::dora::Camera::from(rendertarget_get_camera(self.raw())) };
	}
	/// Gets the texture generated by the rendering target.
	pub fn get_texture(&self) -> crate::dora::Texture2D {
		return unsafe { crate::dora::Texture2D::from(rendertarget_get_texture(self.raw())).unwrap() };
	}
	/// Renders a node to the target without replacing its previous contents.
	///
	/// # Arguments
	///
	/// * `target` - The node to be rendered onto the render target.
	pub fn render(&mut self, target: &dyn crate::dora::INode) {
		unsafe { rendertarget_render(self.raw(), target.raw()); }
	}
	/// Clears the previous color, depth and stencil values on the render target.
	///
	/// # Arguments
	///
	/// * `color` - The clear color used to clear the render target.
	/// * `depth` - Optional. The value used to clear the depth buffer of the render target. Default is 1.
	/// * `stencil` - Optional. The value used to clear the stencil buffer of the render target. Default is 0.
	pub fn render_clear(&mut self, color: &crate::dora::Color, depth: f32, stencil: i32) {
		unsafe { rendertarget_render_clear(self.raw(), color.to_argb() as i32, depth, stencil); }
	}
	/// Renders a node to the target after clearing the previous color, depth and stencil values on it.
	///
	/// # Arguments
	///
	/// * `target` - The node to be rendered onto the render target.
	/// * `color` - The clear color used to clear the render target.
	/// * `depth` - The value used to clear the depth buffer of the render target. Default can be 1.
	/// * `stencil` - The value used to clear the stencil buffer of the render target. Default can be 0.
	pub fn render_clear_with_target(&mut self, target: &dyn crate::dora::INode, color: &crate::dora::Color, depth: f32, stencil: i32) {
		unsafe { rendertarget_render_clear_with_target(self.raw(), target.raw(), color.to_argb() as i32, depth, stencil); }
	}
	/// Saves the contents of the render target to a PNG file asynchronously.
	///
	/// # Arguments
	///
	/// * `filename` - The name of the file to save the contents to.
	/// * `handler` - The function to call when the save operation is complete. The function will be passed a boolean value indicating whether the save operation was successful.
	pub fn save_async(&mut self, filename: &str, mut handler: Box<dyn FnMut(bool)>) {
		let mut stack = crate::dora::CallStack::new();
		let stack_raw = stack.raw();
		let func_id = crate::dora::push_function(Box::new(move || {
			handler(stack.pop_bool().unwrap())
		}));
		unsafe { rendertarget_save_async(self.raw(), crate::dora::from_string(filename), func_id, stack_raw); }
	}
	pub fn new(width: i32, height: i32) -> RenderTarget {
		unsafe { return RenderTarget { raw: rendertarget_new(width, height) }; }
	}
}